﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PVFSLEAP
{
	public partial class F_MAIN: Form
	{
		F_SETT	ST = new F_SETT();
		C_COMM	CM = new C_COMM();
		C_NODE	ND = new C_NODE();
		C_MSSG	MG = new C_MSSG();
		F_NDLG	LG = new F_NDLG();
		C_EVNT	ET = new C_EVNT();

		C_RPBS	RP = new C_RPBS();

		C_NODE[]	SN;
		Button[]	BT;

		int		x, y;

		public F_MAIN()
		{
			InitializeComponent();
		}

		private void BT_MAKE_Click( object sender, EventArgs e )
		{
			clearNodes();

			BT = new Button[C_COMM.TTND];
			SN = new C_NODE[C_COMM.TTND];

			createNodes();
			copyLocationInSN();
			measureCost();
			drawRoutingPath();
			getHopCount();

			updateCompromisedNodes();
			updateAdversaryNodes();

			printCompromisedNodes();
		}

		public void clearNodes()
		{
			PN_SNFD.Controls.Clear();
			PN_SNFD.Invalidate( true );
			DG_CPND.RowCount = 1;
		}

		public void createNodes()
		{
			int x1 = 0, x2 = C_COMM.CLST;
			int y1 = 0, y2 = C_COMM.CLST;

			for ( int i = 0; i < C_COMM.TTND; i++ )
			{
				BT[i] = new Button();
				BT[i].Name = i.ToString();

				if ( i == 0 )
				{
					BT[i].Text = "B";
					BT[i].Font = new Font( "맑음고딕", 6.0F );

					BT[i].Width		= 20;
					BT[i].Height	= 20;

					BT[i].Left	= ST.OBSPX;
					BT[i].Top	= ST.OBSPY;
				}
				else
				{
					BT[i].Width = 10;
					BT[i].Height = 10;

					if ( x2 > C_COMM.WDTH )
					{
						x1 = 0;
						x2 = 99;
						y1 = y1 + C_COMM.CLST;
						y2 = y2 + C_COMM.CLST;
					}

					x = ND.randPosX( x1, x2 );
					y = ND.randPosY( y1, y2 );

					x1 = x1 + C_COMM.CLST;
					x2 = x2 + C_COMM.CLST;

					BT[i].Left = x;
					BT[i].Top = y;
				}

				PN_SNFD.Controls.Add( BT[i] );

				BT[i].Click += new System.EventHandler( BT_NODE_Click );
			}
		}

		void updateCompromisedNodes()
		{
			if ( ST.OFRIA == C_COMM.TRUE )
			{
				int j = 1;
				//for ( int i = 0; i < 10000; i++ )
				while ( true )
				{
					try{
					if ( j > ST.OCNFV )
						break;

					int cpnd = ND.getRandom( 1, ST.TTND );

					if ( SN[cpnd].CPNF != true )
					{
						if ( SN[cpnd].HPCT <= 7 )
							continue;

						if ( ND.makeCompromisedNodes( SN, cpnd ) == -1 )
							continue;
						
						SN[cpnd].CPNF = true;

						j++;
					}
					}
					catch
					{
						break;
					}

					//if ( SN[cpnd].HPCT != 11 )
					//{
					//    //SN[cpnd].CPNF = false;
					//    //for ( int c = 0; c < SN[cpnd].CPND.Length; c++ )
					//    //    SN[cpnd].CPND[c] = -1;
					//    continue;
					//}
				}
			}
		}

		void updateAdversaryNodes()
		{
			if ( ST.OWORM == C_COMM.TRUE )
			{
				for ( int i = 0; i < 2; i++ )
				{
					int adnd = ND.getRandom( 1, ST.TTND );

					while ( true )
					{
						if ( SN[adnd].CPNF == true )
							adnd = ND.getRandom( 1, ST.TTND );
						else
							break;
					}

					SN[adnd].WORM = true;
				}
			}
		}

		void printCompromisedNodes()
		{
			int r = 0;
			//DG_CPND.RowCount = ST.OCNFV + 1;

			for ( int i = 0; i < ST.TTND; i++ )
			{
				if ( SN[i].CPNF == true )
				{
					for ( int c = 0; c < SN[i].CPND.Length; c++ )
					{
						try {
							DG_CPND.Rows.Add( 1 );
							DG_CPND[0, r].Value = i.ToString();
							DG_CPND[1, r].Value = SN[i].CPND[c].ToString();
							r++;
						}
						catch {
							continue;
						}
					}
					changeBTColor( C_COMM.CNMD, i );
				}
			}
		}

		void BT_NODE_Click( object sender, EventArgs e )
		{
			int id = int.Parse( ( ( Button )sender ).Name );

			//F_NDIF NF = new F_NDIF( SN[id].NDID, SN[id].PRID, SN[id].HPCT, SN[id].ENGY, SN[id].MGCT, SN[id].COST );
			F_NDIF NF = new F_NDIF( SN[id].NDID, SN[id].PRID, SN[id].HPCT, SN[id].ENGY, SN[id].COST, SN[id].CHLD );
			NF.StartPosition = FormStartPosition.Manual;
			NF.DesktopLocation = new Point( System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y );
			NF.Owner = this;
			NF.Show();
		}

		public void copyLocationInSN()
		{
			for ( int i = 0; i < C_COMM.TTND; i++ )
			{
				SN[i] = new C_NODE();
				SN[i].setPosition( i, BT[i].Left, BT[i].Top );
			}
		}

		public void measureCost()
		{
			RP.MeasureCost( SN );
		}

		public void getHopCount()
		{
			for ( int i = 1; i < C_COMM.TTND; i++ )
			{
				if ( SN[i].PRID == -1 )
					changeBTColor( C_COMM.NPRT, i );
				else
					SN[i].HPCT = RP.MeasureHopCount( i, SN );
			}
		}			

		public void drawRoutingPath()
		{
			C_RPBS RB = new C_RPBS();
			RB.SetPanel( this.PN_SNFD );
			RB.DrawRoutingPath( SN );
		}

		private void BT_SETT_Click( object sender, EventArgs e )
		{
			ST.ShowDialog();
		}

		private void BT_PLAY_Click( object sender, EventArgs e )
		{
			for ( int i = 1; i < ST.OTTND; i++ )
			{
				if ( SN[i].PRID == -1 )
					return;
			}
			for ( int i = 0; i < ST.OAQOE; i++ )
			{
				if ( ST.OFRIA == C_COMM.TRUE )
				{
					ET.attackFRIA( SN );
				}

				if ( ( i % 200 == 0 ) && ( ST.OWORM == C_COMM.TRUE ) )
				{
					ET.attackWormhole( SN );
				}
				//System.Threading.Thread.Sleep( 1 );
			}
		}

		private void changeBTColor( int C, int I )
		{
			switch ( C )
			{
				// Search Button
				case C_COMM.SCND:
					BT[I].BackColor = Color.Blue;
					BT[I].FlatStyle = FlatStyle.Popup;
					break;
				// Compromised Node
				case C_COMM.CNMD:
					BT[I].BackColor = Color.Yellow;
					BT[I].FlatStyle = FlatStyle.Popup;
					break;
				// Wormhole Node
				case C_COMM.WORM:
					BT[I].BackColor = Color.Red;
					BT[I].FlatStyle = FlatStyle.Popup;
					break;
				// No Parent Node
				case C_COMM.NPRT:
					BT[I].BackColor = Color.Orange;
					BT[I].FlatStyle = FlatStyle.Popup;
					break;
			}
		}

		private void BT_NDLG_Click( object sender, EventArgs e )
		{
			try
			{
				int[]		ndid = new int[ST.OTTND];
				int[]		prid = new int[ST.OTTND];
				int[]		hpct = new int[ST.OTTND];
				double[]	engy = new double[ST.OTTND];
				float[]		cost = new float[ST.OTTND];
				int[]		ovri = new int[ST.OTTND];
				int[]		ofri = new int[ST.OTTND];
				int[]		owrm = new int[ST.OTTND];
				int[]		dfvi = new int[ST.OTTND];
				int[]		dfri = new int[ST.OTTND];
				int[]		dwrm = new int[ST.OTTND];

				for ( int i = 0; i < ST.OTTND; i++ )
				{
					// [ General Inoformation ]
					ndid[i] = SN[i].NDID;
					prid[i] = SN[i].PRID;
					hpct[i] = SN[i].HPCT;
					engy[i] = SN[i].ENGY;
					cost[i] = SN[i].COST;
					ovri[i] = SN[i].OFVI;
					ofri[i] = SN[i].OFRI;
					owrm[i] = SN[i].OWRM;
					dfvi[i] = SN[i].DFVI;
					dfri[i] = SN[i].DFRI;
					dwrm[i] = SN[i].DWRM;
				}

				//LG = new F_NDLG( SMND, ndid, prid, hpct, engy, cost, mgct, chld, ctfr, ctfm, rper, dfrc, dfmc, spid, skey, indk, prwk, clsk, grpk );
				LG = new F_NDLG( ST.OTTND, ndid, prid, hpct, engy, cost, ovri, ofri, owrm, dfvi, dfri, dwrm );
				LG.Show();
			}
			catch
			{
				MessageBox.Show( MG.MAIN_OPEN_NDLG );
				return;
			}
		}
	}
}
